--[[doc
h1. Plugin API: Space

*Description:*
*	a) Creates a fontstring which shows formatted text
*	b) Makes a given frame into a space updater

*Spawn with:*
*	:SpawnPlugin("Space", text [, bags]): Create a formatted space display (for bags, see cargBags:ParseBags(bags))
*	:SpawnPlugin("Space", frame [, bags]): Make this frame into a space updater (for bags, see cargBags:ParseBags(bags))

*Space properties:*
*	.Text: Holds the formatted text, with tags [free], [used] and [max] (Default: "[free] / [max]")

*Space callback functions:*
*	:UpdateText(free, max): overwrite this for custom functions
doc]]

-- Update the space display
local function updater(self, event)
	if(event == "ITEM_LOCK_CHANGED" or event == "BAG_UPDATE_COOLDOWN"  or event == "PLAYER_MONEY") then return end

	local max = 0
	local free = 0
	local handler = cargBags:GetHandler()
	if(self.Bags) then
		for _, id in pairs(self.Bags) do
			free = free + handler.GetContainerNumFreeSlots(id)
			max = max + handler.GetContainerNumSlots(id)
		end
	elseif(self.Object.Bags) then
		for id, _ in pairs(self.Object.Bags) do
			if(tonumber(id)) then
				free = free + handler.GetContainerNumFreeSlots(id)
				max = max + handler.GetContainerNumSlots(id)
			end
		end
	end

	if(self.UpdateText) then return self:UpdateText(free, max) end
	if(not self.Text) then return end

	local text = self.Text:gsub("%[free%]", free)
	text = text:gsub("%[used%]", max-free)
	text = text:gsub("%[max%]", max)
	self:SetText(text)
end

-- Register the plugin
cargBags:RegisterPlugin("Space", function(self, arg1, bagType)
	local plugin
	if(type(arg1) == "table") then -- Custom frame
		plugin = arg1
	else -- Default frame
		plugin = self:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
		plugin.Text = arg1 or "[free] / [max]"
	end
	plugin.Object = self

	local table, min, max = cargBags:ParseBags(bagType)
	local bags = table or min and {}
	plugin.Bags = bags
	if(not table) then
		if(max) then
			for i=min, max do
				bags[#bags+1] = i
			end
		elseif(min) then
			bags[#bags+1] = min
		end
	end

	cargBags:AddCallback(plugin, updater)
	return plugin
end)